<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Other Behaviors</title>

    <style type="text/css">
        <!--
        .style1 {
            font-family: "Courier New", Courier, monospace
        }

        -->
    </style>
</head>
<body>
<h3>Property Applying</h3>

<p>This is where there are a number of setters for a component
    that will be could be set after instantiation. A way of handing in some
    configuration if you like.</p>

<div class="source">
<pre>class Foo {
 String message;
 public void setMessage(String message) {
 this.message = message;
 }
 public String toString() {
 return message;
 }
}
...
pico = new DefaultPicoContainer(new PropertyApplying());
pico.addComponent(Foo.class);
PropertyApplicator pa = (PropertyApplicator) pico.getComponentAdapter(Foo.class);
pa.setProperty(&quot;message&quot;, &quot;hello&quot;);
System.out.println(pico.getComponent(Foo.class)); // prints hello
</pre>
</div>
<div class="source">
<pre>pico = new DefaultPicoContainer();
pico.as(APPLY_PROPERTIES).addComponent(Foo.class);
PropertyApplicator pa = (PropertyApplicator) pico.getComponentAdapter(Foo.class);
pa.setProperty(&quot;message&quot;, &quot;hello&quot;);
System.out.println(pico.getComponent(Foo.class)); // prints hello
</pre>
</div>
<div class="source">
<pre>pico = new PicoBuilder().withProperties().build();
pico.addComponent(Foo.class);
PropertyApplicator pa = (PropertyApplicator) pico.getComponentAdapter(Foo.class);
pa.setProperty(&quot;message&quot;, &quot;hello&quot;);
System.out.println(pico.getComponent(Foo.class)); // prints hello
</pre>
</div>
<div class="source">
<pre>import static org.picocontainer.behaviors.Behaviors.propertyApplying;
...
pico = new PicoBuilder().withBehaviors(propertyApplying()).build();
pico.addComponent(Foo.class);
PropertyApplicator pa = (PropertyApplicator) pico.getComponentAdapter(Foo.class);
pa.setProperty(&quot;message&quot;, &quot;hello&quot;);
System.out.println(pico.getComponent(Foo.class)); // prints hello
</pre>
</div>
Handling Behavior Chains
<div class="source">
<pre>import static org.picocontainer.behaviors.Behaviors.caching;
import static org.picocontainer.behaviors.Behaviors.propertyApplying;
...
pico = new PicoBuilder().withBehaviors(caching(), propertyApplying()).build();
pico.addComponent(Foo.class);
Cached cached = (Cached) pico.getComponentAdapter(Foo.class);
PropertyApplicator pa = (PropertyApplicator) getDelegate(PropertyApplicator.class);
pa.setProperty(&quot;message&quot;, &quot;hello&quot;);
System.out.println(pico.getComponent(Foo.class)); // prints hello
</pre>
</div>
<h3>Automatic</h3>

<p>This is where a component is going to be instantiated
    regardless of whether:</p>
<ul>
    <li> it is looked up</li>
    <li> any component that needs it is looked up</li>
    <li>it is Startable</li>
</ul>
<p>It is most likely that you're doing this because the component
    in question is self contained and doing something once only.
    Alternatively, you're cutting a legacy codebase over from
    nest-of-singletons to dependency injection in stages.</p>

<div class="source">
<pre>class Foo {

 public Foo() {
 System.out.println(&quot;Foo was instantiated&quot;);
 }
}

...
pico = new DefaultPicoContainer(new Automatic());
pico.addComponent(Foo.class);
pico.addComponent(&quot;bar&quot;, String.class);
pico.getComponent(&quot;bar&quot;); // Foo instantiated too.
</pre>
</div>
<div class="source">
<pre>pico = new DefaultPicoContainer();
pico.as(AUTOMATIC).addComponent(Foo.class);
pico.addComponent(&quot;bar&quot;, String.class);
pico.getComponent(&quot;bar&quot;); // Foo instantiated too.
</pre>
</div>
<div class="source">
<pre>pico = new PicoBuilder().withAutomatic().build();
pico.addComponent(Foo.class);
pico.addComponent(&quot;bar&quot;, String.class);
pico.getComponent(&quot;bar&quot;); // Foo instantiated too.
</pre>
</div>
<div class="source">
<pre>import static org.picocontainer.behaviors.Behaviors.automatic;
...
pico = new PicoBuilder().withBehaviors(automatic()).build();
pico.addComponent(Foo.class);
pico.addComponent(&quot;bar&quot;, String.class);
pico.getComponent(&quot;bar&quot;); // Foo instantiated too.
</pre>
</div>
</body>
</html>